; $(function () {
    //轮播图
    var Index = 0;
    var imgwidth=992;
    var circle=$(".circle li")
    //自动播放
    var timer = setInterval(function () {
        rightjieliu();
    }, 2000);

    $(".banner_wrapper").on("mouseenter", function () {
        $(".left,.right").stop().fadeIn(1000);
        clearInterval(timer);
    }).on("mouseleave", function () {
        timer = setInterval(rightjieliu(), 2000);
    })
    $(".all").on("mouseleave",function () {
        $(".left,.right").stop().fadeOut(1000);
    })
    // 移入左右键清楚定时器
    $(".left,.right").on("mouseenter", function () {
        clearInterval(timer);
    }).on("mouseleave", function () {
        timer = setInterval(rightjieliu(), 1500);
    })
    //移入小圆点清除定时器
    $(".circle").on("mouseenter",function () {
        clearInterval(timer);
    }).on("mouseleave",function () {
        timer = setInterval(rightjieliu(), 1500);
    })
    //左边点击
    $(".left").on("click", leftjieliu());
    //右边点击
    $(".right").click(rightjieliu());
    //左边left节流
    function leftjieliu() {
        var oTimer1;
        return function () {
            clearTimeout(oTimer1);
            //延时器
            oTimer1 = setTimeout(function () {
                if (Index == 0) {
                    Index = 5
                    $(".bannerImg").css({
                        left: imgwidth * -Index
                    })
                }
                Index--;
                $(".bannerImg").animate({
                    left: imgwidth * -Index
                }, 200);
                if(Index==0){
                    circle.eq(0).addClass("current").siblings("li").removeClass("current");
                }else{
                    circle.eq(Index).addClass("current").siblings("li").removeClass("current");
                }
            }, 200)
        }
    }
    //右边right节流
    function rightjieliu() {
        var oTimer1;
        return function () {
            clearTimeout(oTimer1);
            oTimer1 = setTimeout(function () {
                if (Index == 5) {
                    Index = 0;
                    $(".bannerImg").css({
                        left: 0
                    })
                }
                Index++;
                $(".bannerImg").animate({
                    left: imgwidth * -Index
                }, 200);
                if(Index==5){
                    circle.eq(0).addClass("current").siblings("li").removeClass("current");
                }else{
                    circle.eq(Index).addClass("current").siblings("li").removeClass("current");
                }
            }, 200)
        }
    }
    //点击小圆点
    circle.on("click",function () {
    console.log($(this))
    $(this).addClass("current").siblings("li").removeClass("current");
    Index=$(this).index();
    $(".bannerImg").stop().animate({
        left:-Index*imgwidth
    })
})

//热卖排行榜分页切换
$(".con_tit").find("li").on("mouseenter",function () {
    var index=$(this).index();
    $(".my_hot_con").find(".m-h-items").eq(index).show().siblings(".m-h-items").hide();
    $(this).addClass("current").siblings("li").removeClass("current");
});

//点击更多加载商品
    $(".goods-hide").hide();
    $(".more").on("click",function () {
        $(".goods-hide").show();
    });

    //侧边导航栏
    // 购物车
    $("#sidebar-cart").on("mouseenter",function () {
        $(".side-panel-item-cart").show();
    });
    //移出购物车列表隐藏
    $("#sidebar-cart,.side-panel-item-cart").on("mouseleave",function() {
        $(".side-panel-item-cart").hide();
    });

    // 移入手机选项，跳出app二维码
    $(".side-panel-App").on("mouseenter",function () {
        $(".side-panel-phone").show();
    });
    // 移出手机隐藏
    $(".side-panel-App,.side-panel-phone").on("mouseleave",function () {
        $(".side-panel-phone").hide();
    })
    //移到客服图标变文字
    $("#kfzx").on("mouseenter",function () {
        $(".icon-kefu").hide();
        $(".tip-txt").show();
    })
    $("#kfzx").on("mouseleave",function () {
        $(".tip-txt").hide();
        $(".icon-kefu ").show();
    });
    // 回顶上
    $("#elevator").on("mouseenter",function () {
        $(".icon-top").hide();
        $(".top-txt").show();
    })
    $("#elevator").on("mouseleave",function () {
        $(".top-txt").hide();
        $(".icon-top").show();
    })
    //点击回到顶上到最上面
    $("#elevator").on("click",function () {
        $("html").animate({scrollTop:0},500);
        return false;
    })

    //延迟加载图片
    // $("img").attr("data-original")
    $("img").lazyload({
        placeholder:"../images/loading.gif",
        effect: "fadeIn",
        threshold: -70,
    },function () {
        alert("lazy启动完毕")
    })
    
    //渲染商品数据
    $.ajax({
        url:"../../api/goods",
        dataType:"json",
        type:"get"
    }).done((data)=>{
        console.log(data);
        data.forEach((el)=>{
            var strHTML=`<a class="m-sale clearFixed view-EventListItem common-item-show" clog="" c-emit="show;click" c-et="event"
            c-etl="108509014" href="./goodsdetails.html?pid=${el.pid}" target="_blank"
            clog-get="true">
            <div class="m-item-show-wrap">
                <div class="m-item-detail">
                    <div class="top-product">今日已抢
                        <span class="top-num">${el.topNum[0]}</span>
                        <span class="top-num">${el.topNum[1]}</span>
                        <span class="top-num">${el.topNum[2]}</span>
                        <span class="top-num">${el.topNum[3]}</span>
                        件
                    </div>
                    <img class="lazy" src="${el.middle_img[0]}""
                        style="display: inline;">
                    <p class="title">${el.pName}</p>
                    <div class="price-info">
                        <p class="cur-price">
                            <span class="currency">¥</span><span class="price-num">${el.pirce}</span>
                        </p>
                        <p class="old-price">${el.originP}</p>
                        <p class="discount-desc">${el.desc}</p>
                        <p>
                        </p>
                    </div>
                </div>
            </div>
        </a>`
        $(".goodsshow").append($(strHTML));
        })


           //侧边购物车渲染
            // 侧边购物车渲染
        async function show() {
            var result = await $.get("../../api/getcarts", {
                uid: JSON.parse($.cookie("userinfo") || "[]").uId,
            })
            num = result.length;
            if (result.length == 0) {
                $(".empty-cart").show();
                $(".carlis").hide();
            } else {
                $(".empty-cart").hide();
                $(".carlis").show();
                window.localStorage.setItem("shopcars1", JSON.stringify(result)||"[]")
                var sidecart2=``;
                result.forEach(el => {
                   sidecart2 += `
                <li class="carlis" wg-view="item" style="display: list-item; padding: 12px; list-style: none; position: relative; overflow: hidden; height: 60px; border-bottom: none;"><a href="//www.beibei.com/detail/200433472-11353645.html" wg-view="imglink" op-value="itemLink" op-attr="href"><img wg-view="img" op-value="imgUrl" op-attr="src" src="..${el.pImg}" style="border: 2px solid rgb(204, 204, 204); background: rgb(255, 255, 255); padding: 2px; width: 50px; height: 50px; position: relative; z-index: 4;"></a><a href="//www.beibei.com/detail/200433472-11353645.html" wg-view="name" op-value="title,itemLink" op-attr="*,href" style="position: absolute; left: 0px; top: 15px; padding: 0px 10px 0px 80px; line-height: 1.2em; height: 30px; overflow: hidden; font-size: 13px; margin: 0px; width: 50%; z-index: 3;">${el.pName}SPF25/PA++30ml</a><p wg-view="sku" op-value="sku_props" style="position: absolute; color: rgb(170, 170, 170); left: 0px; top: 52px; padding: 0px 10px 0px 80px; line-height: 1.2em; height: 14px; overflow: hidden; font-size: 12px; margin: 0px; width: 50%;">颜色:${el.pSize}</p><p wg-view="price" op-value="price" style="position: absolute; color: rgb(255, 84, 130); right: 15px; top: 25px; overflow: hidden; font-size: 16px; padding-left: 80px; margin: 0px; width: 30%; line-height: 1em; text-align: right;">¥${el.pPrice}<br><span wg-view="num" style="display: inline; color: rgb(136, 136, 136); font-size: 13px;"> x${el.pNum}</span></p></li>
                   `
                    //加减
                })
                $(".cartInfor2").append(sidecart2);
                //取出购物车对象,循环,计算总价
                let shopcarts = JSON.parse(window.localStorage.getItem("shopcars1")||"[]");
                var shopcartsTotalPrice = 0;
                shopcarts.map((item) => {
                    shopcartsTotalPrice += item.pNum * parseInt(item.pPrice)
                })
            }
            $(".headcarT").text(shopcartsTotalPrice)
            $(".goodnum").text(num)
            console.log(num)
        }
        show()
    })
    
       

})
